<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <title>Arduino - Print </title>
  <link rel='stylesheet' href='arduino.css' type='text/css' />
</head>
<body>
<div id="page">
<!--PageHeaderFmt-->
<div id="pageheader">
  <div class="title"><a href="http://www.arduino.cc"/>Arduino</a></div>
  <div class="search">
    <!-- SiteSearch Google -->    <FORM method=GET action="http://www.google.com/search">    <input type=hidden name=ie value=UTF-8>    <input type=hidden name=oe value=UTF-8>    <INPUT TYPE=text name=q size=25 maxlength=255 value="">    <INPUT type=submit name=btnG VALUE="search">    <input type=hidden name=domains value="http://www.arduino.cc/">
    <input type=hidden name=sitesearch value="http://www.arduino.cc/">
    </FORM>    <!-- SiteSearch Google -->
  </div>
</div>
<!--/PageHeaderFmt-->
<!--PageLeftFmt-->
<div id="pagenav" style="text-align: right">
  <div style="float: left;">
  <p><a class='wikilink' href='http://www.arduino.cc/en/Main/Buy'>Buy</a>
|
<a class='wikilink' href='http://www.arduino.cc/en/Main/Software'>Download</a>
|
<a class='wikilink' href='Guide_index.html'>Getting Started</a>
|
<a class='wikilink' href='http://www.arduino.cc/en/Tutorial/HomePage'>Learning</a>
|
<a class='wikilink' href='index.html'>Reference</a>
|
<a class='wikilink' href='http://www.arduino.cc/en/Main/Hardware'>Hardware</a>
|
<a class='wikilink' href='FAQ.html'>FAQ</a>
</p>
<p class='vspace'></p>

  </div>
  <a class="urllink" href="http://www.arduino.cc/blog/" rel="nofollow">Blog &raquo;</a> |
  <a class="urllink" href="http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl" rel="nofollow">Forum &raquo;</a> |
  <a class="urllink" href="http://www.arduino.cc/playground/" rel="nofollow">Playground &raquo;</a>
</div>
<!--/PageLeftFmt-->
<div id="pagetext">
<!--PageText-->
<div id='wikitext'>
<h2>Serial.print(data)</h2>
<h4>Description</h4>
<p>Prints data to the serial port. 
</p>
<p class='vspace'></p><h4>Parameter</h4>
<p>data: all integer types, including char
</p>
<p class='vspace'></p><h4>Syntax</h4>
<p>This command can take many forms:
</p>
<p class='vspace'></p><p><strong>Serial.print(b)</strong> 
with no format specified, prints b as a decimal number in an ASCII string.  For example, 
</p>
<p class='vspace'></p><pre>
int b = 79;
Serial.print(b);
</pre><p>prints the ASCII string "79".
</p>
<p class='vspace'></p><p><strong>Serial.print(b, DEC)</strong> 
prints b as a decimal number in an ASCII string.  For example, 
</p>
<p class='vspace'></p><pre>
int b = 79;
Serial.print(b, DEC);
</pre><p>prints the string "79".
</p>
<p class='vspace'></p><p><strong>Serial.print(b, HEX)</strong> 
prints b as a hexadecimal number in an ASCII string.  For example, 
</p>
<p class='vspace'></p><pre>
int b = 79;
Serial.print(b, HEX);
</pre><p>prints the string "4F".
</p>
<p class='vspace'></p><p><strong>Serial.print(b, OCT)</strong> 
prints b as an octal number in an ASCII string.  For example, 
</p>
<p class='vspace'></p><pre>
int b = 79;
Serial.print(b, OCT);
</pre><p>prints the string "117".
</p>
<p class='vspace'></p><p><strong>Serial.print(b, BIN)</strong> 
prints b as a binary number in an ASCII string.  For example, 
</p>
<p class='vspace'></p><pre>
int b = 79;
Serial.print(b, BIN);
</pre><p>prints the string "1001111".
</p>
<p class='vspace'></p><p><strong>Serial.print(b, BYTE)</strong> 
prints b as a single byte.  For example, 
</p>
<p class='vspace'></p><pre>
int b = 79;
Serial.print(b, BYTE);
</pre><p>returns the string "O", which is the ASCII character represented by the value 79.  For more information see the <a class='urllink' href='http://www.asciitable.com' rel='nofollow'>ASCII table</a>.
</p>
<p class='vspace'></p><p><strong>Serial.print(str)</strong> 
if str is a string or an array of chars, prints str as an ASCII string.  For example, 
</p>
<p class='vspace'></p><pre>
Serial.print("Hello World!");
</pre><p>prints the string "Hello World!".
</p>
<p class='vspace'></p><h4>Parameters</h4>
<p>b: the byte to print, or
</p>
<p class='vspace'></p><p>str: the string to print
</p>
<p class='vspace'></p><h4>Returns</h4>
<p>None
</p>
<p class='vspace'></p><h4>Example:</h4>
<pre>
/*
  Analog input

 reads an analog input on analog in 0, prints the value out.

 created 24 March 2006
 by Tom Igoe
 */

int analogValue = 0;    // variable to hold the analog value

void setup() {
  // open the serial port at 9600 bps:
  Serial.begin(9600);
}

void loop() {
  // read the analog input on pin 0:
  analogValue = analogRead(0);

  // print it out in many formats:
  Serial.print(analogValue);         // print as an ASCII-encoded decimal
  Serial.print("\t");                // print a tab character
  Serial.print(analogValue, DEC);    // print as an ASCII-encoded decimal
  Serial.print("\t");                // print a tab character
  Serial.print(analogValue, HEX);    // print as an ASCII-encoded hexadecimal
  Serial.print("\t");                // print a tab character
  Serial.print(analogValue, OCT);    // print as an ASCII-encoded octal
  Serial.print("\t");                // print a tab character
  Serial.print(analogValue, BIN);    // print as an ASCII-encoded binary
  Serial.print("\t");                // print a tab character
  Serial.print(analogValue/4, BYTE); // print as a raw byte value (divide the
                                     // value by 4 because analogRead() returns numbers
                                     // from 0 to 1023, but a byte can only hold values
                                     // up to 255)
  Serial.print("\t");                // print a tab character    
  Serial.println();                  // print a linefeed character

  // delay 10 milliseconds before the next reading:
  delay(10);
}
</pre>
<p class='vspace'></p><h4>Programming Tips / Known Issues</h4>
<p>Serial.print() doesn't work on floats, so you'll need to cast them to an integral type, losing any fractional values. It is sometimes useful to multiply your float by a power of ten, to preserve some of this fractional resolution.
</p>
<p class='vspace'></p><p>Be careful about doing math inside the brackets e.g.<br /><code>Serial.print(x-2, DEC);</code><br />The unsigned char data type, and byte data type will yield incorrect results and act as though they are signed types i.e. type char.
</p>
<p class='vspace'></p><p>The Serial.Print function puts data into a buffer. It will wait for one character to send, before going on to the next character. However the function returns before sending the <em>last</em> character.
</p>
<p class='vspace'></p><h4>See also</h4>
<ul><li><a class='wikilink' href='ASCIIchart.html'>ASCII chart</a>
</li><li><a class='wikilink' href='Serial_Begin.html'>Serial.begin</a>(speed)
</li><li><a class='wikilink' href='Serial_Available.html'>Serial.available</a>()
</li><li><a class='wikilink' href='Serial_Read.html'>Serial.read</a>()
</li><li><a class='wikilink' href='Serial_Println.html'>Serial.println</a>(data)
</li></ul><p class='vspace'></p><p><a class='wikilink' href='index.html'>Reference Home</a>
</p>
</div>

</div>
<!--PageFooterFmt-->
<div id="pagefooter">
  <a href='Serial_Print?action=edit.html'>Edit Page</a> | <a href='Serial_Print?action=diff.html'>Page History</a> | <a href='Serial_Print?action=print.html' target='_blank'>Printable View</a> | <a href='http://www.arduino.cc/en/Site/AllRecentChanges'>All Recent Site Changes</a>
</div>
<!--/PageFooterFmt-->
</div>
</body>
</html>
